// DlgPref.cpp: implementation of the CDlgPref class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CoodTimer.h"
#include "DlgPref.h"
#include <commctrl.h>
#include "storage.h"
#include "WaitCursor.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COLORREF CDlgPref::m_arc[16]={
  RGB(255,255,0),
  RGB(255,0,0),
  RGB(255,255,255),
  RGB(0,0,0),
  RGB(255,255,255),
  RGB(0,0,0),
  RGB(255,255,255),
  RGB(0,0,0),
  RGB(255,255,255),
  RGB(0,0,0),
  RGB(255,255,255),
  RGB(0,0,0),
  RGB(255,255,255),
  RGB(0,0,0),
  RGB(255,255,255),
  RGB(0,0,0)
};
static CDlgPref* g_pdlg=NULL;
CDlgPref::CDlgPref(const CDraw* pdr,const CAlarm* pal)
:m_dr(*pdr),m_al(*pal),
m_nTic(0),
m_w(pdr->m_nWidth)
{

}

CDlgPref::~CDlgPref()
{

}
UINT CDlgPref::DoModal(HWND hWndParent)
{
  if(g_pdlg!=NULL)
    return IDCANCEL;
  g_pdlg=this;
  UINT ret= DialogBox(g_hInst, (LPCTSTR)IDD_PREF, hWndParent, (DLGPROC)_DlgProc);
  g_pdlg=NULL;
  return ret;

}
LRESULT CALLBACK CDlgPref::_DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  g_pdlg->m_hWnd=hDlg;
  return g_pdlg->DlgProc( message, wParam, lParam);
}
LRESULT CDlgPref::DlgProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_COMMAND:
       OnCommand(LOWORD(wParam),HIWORD(wParam));
       break;
    case WM_INITDIALOG:   OnCreate();  return TRUE;
		//case WM_PAINT:   OnPaint();		break;
		case WM_TIMER:OnTimer(wParam);	break;
		case WM_DESTROY:OnDestroy();	break;
   }
   return 0;
}
void CDlgPref::OnCommand(int wmId,int wmEvent)
{
	// Parse the menu selections:
	switch (wmId)
	{
		case IDCANCEL:
       EndDialog(m_hWnd,IDCANCEL);
			 break;
		case IDOK:
       OnOk();
			 break;
    case IDC_B_BKG:
      OnButtonBKG();
      break;
    case IDC_B_FOR:
      OnButtonFOR();
      break;
    case IDC_R_LEFT_1:
      ChangePosition(epLeft);
      break;
    case IDC_R_RIGHT_1:
      ChangePosition(epRight);
      break;
    case IDC_R_TOP_1:
      ChangePosition(epTop);
      break;
    case IDC_R_BOTTOM_1:
      ChangePosition(epBottom);
      break;
    case IDC_E_W:
      if(wmEvent==EN_CHANGE)
        OnChangeWidth();
      break;
	case IDC_CH_MESS:
		OnChMess();
		break;
	case IDC_CH_SOUND:
		OnChSound();
		break;
	case IDC_CH_START:
		OnChStart();
		break;
     case IDC_B_BROWSE:
       OnBrowseSound();
       break;
     case IDC_B_BROWSE_START:
       OnBrowseStart();
       break;
     case IDC_B_TEST:
       OnBTest();
       break;
  }
}
void CDlgPref::OnCreate()
{
  CheckRadioButton(m_hWnd,IDC_R_LEFT_1,IDC_R_BOTTOM_1,
    m_dr.m_ePosition==epLeft ? IDC_R_LEFT_1:
    (m_dr.m_ePosition==epRight ? IDC_R_RIGHT_1:
    (m_dr.m_ePosition==epTop ? IDC_R_TOP_1 :IDC_R_BOTTOM_1)));
  SetTimer(m_hWnd,100,250,NULL);
  SendDlgItemMessage(m_hWnd,IDC_SPIN1,UDM_SETRANGE, 0, MAKELPARAM(20, 1));
  SendDlgItemMessage(m_hWnd,IDC_SP_DUR,UDM_SETRANGE, 0, MAKELPARAM(600, 0));
  SetDlgItemInt(m_hWnd,IDC_E_DURATION,m_al.m_Duration,true);
  InitPage();
  SetDlgItemInt(m_hWnd,IDC_E_W,m_w,true);
}
void CDlgPref::OnOk()
{
  if(GetPage())
	EndDialog(m_hWnd,IDOK);
}
void CDlgPref::OnButtonBKG()
{
  CHOOSECOLOR ch;
  ZeroMemory(&ch,sizeof(CHOOSECOLOR));
  ch.lStructSize=sizeof(CHOOSECOLOR);
  ch.hwndOwner=m_hWnd;
  ch.rgbResult=m_dr.GetB();
  ch.Flags=CC_RGBINIT|CC_ANYCOLOR|CC_FULLOPEN;
  ch.lpCustColors=m_arc;
  if(::ChooseColor(&ch))
    m_dr.SetB(ch.rgbResult);

}
void CDlgPref::OnButtonFOR()
{
  CHOOSECOLOR ch;
  ZeroMemory(&ch,sizeof(CHOOSECOLOR));
  ch.lStructSize=sizeof(CHOOSECOLOR);
  ch.hwndOwner=m_hWnd;
  ch.rgbResult=m_dr.GetF();
  ch.Flags=CC_RGBINIT|CC_ANYCOLOR|CC_FULLOPEN;
  ch.lpCustColors=m_arc;
  if(::ChooseColor(&ch))
    m_dr.SetF(ch.rgbResult);
}
void CDlgPref::ChangePosition(EPosition newPos)
{
  if(m_dr.m_ePosition == newPos)
    return;
  m_dr.m_ePosition = newPos;
  RECT rcFrame;
  GetWindowRect(GetDlgItem(m_hWnd,IDC_FRAME),&rcFrame);
  ScreenToClient(m_hWnd,(POINT*)&rcFrame);
  InvalidateRect(m_hWnd,&rcFrame,TRUE);

}
#define COUNT_TIC 10
void CDlgPref::OnTimer(long id)
{
  m_nTic++;
  if(m_nTic>COUNT_TIC)
    m_nTic=0;
  RECT rcFrame;
  GetWindowRect(GetDlgItem(m_hWnd,IDC_FRAME),&rcFrame);
  ScreenToClient(m_hWnd,(POINT*)&rcFrame);
  ScreenToClient(m_hWnd,(POINT*)&rcFrame.right);
  RECT rcDraw=rcFrame;
  bool bVert=true;
  switch(m_dr.m_ePosition)
  {
  case epLeft:
    rcDraw.right=rcDraw.left+m_dr.m_nWidth;
    break;
  case epRight:
    rcDraw.left=rcDraw.right-m_dr.m_nWidth;
    break;
  case epTop:
    bVert=false;
    rcDraw.bottom=rcDraw.top+m_dr.m_nWidth;
    break;
  case epBottom:
    bVert=false;
    rcDraw.top=rcDraw.bottom-m_dr.m_nWidth;
    break;
  }
  rcFrame=rcDraw;
  int width=max(rcDraw.right-rcDraw.left,rcDraw.bottom-rcDraw.top);
  int cur=m_nTic*width/COUNT_TIC;

  HDC hdc=GetDC(m_hWnd);
  if(bVert)
  {
    rcDraw.bottom=rcDraw.bottom-cur;
    FillRect(hdc,&rcDraw,m_dr.m_hbB);
    rcDraw.top=rcDraw.bottom;
    rcDraw.bottom=rcFrame.bottom;
    FillRect(hdc,&rcDraw,m_dr.m_hbF);
  }
  else
  {
    rcDraw.left=rcDraw.left+cur;
    FillRect(hdc,&rcDraw,m_dr.m_hbB);
    rcDraw.right=rcDraw.left;
    rcDraw.left=rcFrame.left;
    FillRect(hdc,&rcDraw,m_dr.m_hbF);
  }

  ReleaseDC(m_hWnd,hdc);

}
void CDlgPref::OnChangeWidth()
{
  int n=GetDlgItemInt(m_hWnd,IDC_E_W,NULL,true);
  if(n<1)
    n=1;
  if(n>20)
  {
    n=20;
    SetDlgItemInt(m_hWnd,IDC_E_W,n,FALSE);
  }
  m_dr.m_nWidth=n;
  RECT rcFrame;
  GetWindowRect(GetDlgItem(m_hWnd,IDC_FRAME),&rcFrame);
  ScreenToClient(m_hWnd,(POINT*)&rcFrame);
  InvalidateRect(m_hWnd,&rcFrame,TRUE);
}

bool CDlgPref::GetPage()
{
	CStorage st("Pref");
	
	char s[MAX_PATH];
	
	
    BOOL bTr=FALSE;
    m_al.m_Duration=GetDlgItemInt(m_hWnd,IDC_E_DURATION,&bTr,TRUE);
	if(!bTr)
		m_al.m_Duration=-1;
	if(m_al.m_Duration <0  ||m_al.m_Duration>=1200)
	{
		char s[256];
		LoadString(g_hInst,IDS_BAD_DURATION,s,sizeof(s));
		MessageBox(m_hWnd,s,g_szTitle,MB_OK|MB_ICONINFORMATION);
		SetFocus(GetDlgItem(m_hWnd,IDC_E_DURATION));
		return false;
		
	}

	GetDlgItemText(m_hWnd,IDC_CM_ALARM,s,sizeof(s));
	m_al.SetMessage(s);
	st.PutCombo("Mess",GetDlgItem(m_hWnd,IDC_CM_ALARM),20);
	
	GetDlgItemText(m_hWnd,IDC_CM_SOUND,s,sizeof(s));
	m_al.SetSound(s);
	st.PutCombo("Sound",GetDlgItem(m_hWnd,IDC_CM_SOUND),20);
	
	GetDlgItemText(m_hWnd,IDC_CM_START,s,sizeof(s));
    m_al.SetStart(s);
    st.PutCombo("Start",GetDlgItem(m_hWnd,IDC_CM_START),20);
	
	return true;
}
void CDlgPref::InitPage()
{
  CStorage st("Pref");

  CheckDlgButton(m_hWnd,IDC_CH_MESS, m_al.m_bMessage);
  st.GetCombo("Mess",GetDlgItem(m_hWnd,IDC_CM_ALARM),20);
  SetDlgItemText(m_hWnd,IDC_CM_ALARM,m_al.GetMessage());
  OnChMess();

  CheckDlgButton(m_hWnd,IDC_CH_SOUND,m_al.m_bSound);
  st.GetCombo("Sound",GetDlgItem(m_hWnd,IDC_CM_SOUND),20);
  SetDlgItemText(m_hWnd,IDC_CM_SOUND,m_al.GetSound());
  OnChSound();

  CheckDlgButton(m_hWnd,IDC_CH_START,m_al.m_bStart);
  st.GetCombo("Start",GetDlgItem(m_hWnd,IDC_CM_START),20);
  SetDlgItemText(m_hWnd,IDC_CM_START,m_al.GetStart());
  OnChStart();

}

void CDlgPref::OnBrowse(UINT idCmbo,UINT idFilter)
{
  OPENFILENAME of;
  char sFilter[120];
  char sFile[MAX_PATH];
  GetDlgItemText(m_hWnd,idCmbo,sFile,sizeof(sFile));
  LoadString(g_hInst,idFilter,sFilter,sizeof(sFilter));
  for(char* p=sFilter;*p!=0;p++)
    if(*p=='|') 
      *p=0;
  ZeroMemory(&of,sizeof(OPENFILENAME));
  of.lStructSize=sizeof(of);
  of.hwndOwner=m_hWnd;
  of.lpstrFilter=sFilter;
  of.lpstrFile=sFile;
  of.nMaxFile =sizeof(sFile);
  of.Flags=OFN_FILEMUSTEXIST|OFN_NOCHANGEDIR|OFN_HIDEREADONLY;
  if(GetOpenFileName(&of))
  {
    SetDlgItemText(m_hWnd,idCmbo,of.lpstrFile);
  }



}
void CDlgPref::OnBTest()
{
  GetPage();
  m_al.Alarm(false);
	/*
  switch (m_nPage)
  {
    case 0:
      MessageBox(m_hWnd,m_al.GetMessage(),g_szTitle,MB_OK|MB_ICONINFORMATION);
      break;
    case 1:
      {
        CWaitCursor wc;
        DWORD dw=GetTickCount();
        PlaySound(m_al.GetSound(),NULL,SND_FILENAME);
        if(GetTickCount()<dw+4000)
        {
          PlaySound(m_al.GetSound(),NULL,SND_FILENAME);
          PlaySound(m_al.GetSound(),NULL,SND_FILENAME);
          PlaySound(m_al.GetSound(),NULL,SND_FILENAME);
        }
      }
      break;
    case 2:
      ShellExecute(m_hWnd,NULL,m_al.GetStart(),NULL,NULL,SW_SHOWDEFAULT );
      break;
  }
  */
}

void CDlgPref::OnChMess()
{
	m_al.m_bMessage=IsDlgButtonChecked(m_hWnd,IDC_CH_MESS)!=0;
	EnableWindow(GetDlgItem(m_hWnd,IDC_CM_ALARM),m_al.m_bMessage);
}
void CDlgPref::OnChSound()
{
	m_al.m_bSound=IsDlgButtonChecked(m_hWnd,IDC_CH_SOUND)!=0;
	EnableWindow(GetDlgItem(m_hWnd,IDC_CM_SOUND),m_al.m_bSound);
	EnableWindow(GetDlgItem(m_hWnd,IDC_E_DURATION),m_al.m_bSound);
	EnableWindow(GetDlgItem(m_hWnd,IDC_SP_DUR),m_al.m_bSound);
	EnableWindow(GetDlgItem(m_hWnd,IDC_B_BROWSE),m_al.m_bSound);

}
void CDlgPref::OnChStart()
{
	m_al.m_bStart=IsDlgButtonChecked(m_hWnd,IDC_CH_START)!=0;
	EnableWindow(GetDlgItem(m_hWnd,IDC_CM_START),m_al.m_bStart);
	EnableWindow(GetDlgItem(m_hWnd,IDC_B_BROWSE_START),m_al.m_bStart);
}
